<template>
	<el-dialog  :title="title" :visible="showDialog"  @update:visible="updateShowDialog"  @close="beforeClose" >
	    <el-form :model="userData" :rules="userRules" :ref="diyuserRef">
	
		<el-form-item prop="username" label="用户名称">
			<el-input clearable v-model="userData.username" placeholder="用户名称" />
		</el-form-item>
		
		<el-form-item prop="real_name" label="真实名称">
			<el-input autocomplete="off" name="zhenshimingcheng" clearable v-model="userData.real_name" placeholder="真实名称" />
		</el-form-item>
		<el-form-item prop="password" label="密码">
			<el-input autocomplete="new-password" name="xiugaimima" type="password" clearable v-model="userData.password" placeholder="请输入密码" />
		</el-form-item>
	
		<el-form-item prop="password_repeat" label="重复密码">
			<el-input type="password" clearable v-model="userData.password_repeat" placeholder="请再次输入" />
			<!-- <div v-if="showAorB=='B'">
				 <div v-if="confirmPasswordError" style="color: #F56C6C;font-size: 12px;">{{ confirmPasswordError }}</div>
			</div> -->
		</el-form-item>
		<el-form-item prop="phone" label="电话">
			<el-input clearable v-model="userData.phone" placeholder="电话" />
		</el-form-item>
	    
	    </el-form>
	    <div slot="footer" class="dialog-footer">
	      <el-button @click="dCancel($event)">取 消</el-button>
	      <el-button type="primary" @click="dSure($event)">确 定</el-button>
	    </div>
	</el-dialog>
</template>

<script>

export default {

  components: {
	  
  },
  props:{
	  title:String,
	  showDialog:Boolean,
	  diyuserRef:String,
	  userData:Object,
	  userRules:Object
  },
  data() {
    return {
    };
  },
  created() {
  },
  mounted() {
  },
  computed: {
  },
  methods: {
	beforeClose(){ // 右上角×号操作
		this.$emit("dCancel","")
	},
	updateShowDialog(value) { //关闭
		let diyuserRef = this.diyuserRef
		this.$refs[diyuserRef].resetFields();
		this.$emit('update:showDialog', value);
	},
	dCancel(){ //取消
		let diyuserRef = this.diyuserRef
		this.$refs[diyuserRef].resetFields();
		if(this.title == "添加用户"){
			console.log('取消')
			this.$emit("dCancel","add")
		}else{
			this.$emit("dCancel","updata")
		}
	},

	dSure(e){ // 确定
		console.log(84,this.title)
		if(this.title == "添加用户"){
			console.log('确认',e)
			this.adduser()
		}else{
			console.log(88)
			this.updatauser()
		}
		
	},
	updatauser(){ //修改用户
		let that = this;
		let diyuserRef = this.diyuserRef
		that.$refs[diyuserRef].validate( async (valid) => {
		  if (valid) {
			let userData = that.userData 
			console.log(277,userData)
			let id = userData.id
			if(userData.password){
				if(userData.password!=userData.password_repeat){
					return that.$message({
								message: '两次密码不相同',
								type: 'error',
								duration: 1200
							})
				}
			}
			let res = await that.$axios.post( `/api/playlet/dataUser/update/${id}`,userData)
			console.log(334,res)
			that.$message({
				message: '修改成功',
				type: 'success',
				duration: 1200
			})
			that.$refs[diyuserRef].resetFields();
			that.dCancel()
			that.$emit("getData") //调用父组件获取新数据
		  } else {
			that.$message({
				message: '表单填写错误',
				type: 'error',
				duration: 1200
			})
		  }
		});
	},
	adduser(){ //添加用户
		let that = this;
		let userData = that.userData
		let diyuserRef = this.diyuserRef
		that.$refs[diyuserRef].validate( async (valid) => {
		  if (valid) {
			  console.log(userData)
			let res = await that.$axios.post( `/api/playlet/dataUser/create`,userData)
			that.$message({
				message: '添加成功',
				type: 'success',
				duration: 1200
			})
			let diyuserRef = that.diyuserRef
			that.dCancel()
			this.$refs[diyuserRef].resetFields();
			this.$emit("getData") //调用父组件获取新数据
		
		  } else {
			that.$message({
				message: '表单填写错误',
				type: 'error',
				duration: 1200
			})
		  }
		});
	}
	
  },
};
</script>

<style>
	
</style>